const db = require('../config/db.js');
const inFile = require('../utils/inFile.js');

const userModel = {
    login: async ({username,password}) => {

        const sql = `SElECT * FROM user WHERE username = '${username}' AND password =${password};`;
        try {
                const [[{ id='' }]] = await  db.query(sql);
            console.log(id,'登录查询结果');
            if (!!id){
                return id
            }else {
                return 0
            }
        }catch (error) {
            console.log(error,'Model层-用户登录出错啦');
        }


    },
    register: async ({username,password,real_name,age,type,sex,phone,address,avatar},file) =>{
        const sql1 = "SELECT * FROM user WHERE username = ? ";
        const sql2 = "INSERT INTO user (username,password,real_name,age,type,sex,phone,address,avatar) VALUES (?,?,?,?,?,?,?,?,?);";
        const sql3 = "INSERT INTO user (username,password,real_name,age,type,sex,phone,address) VALUES (?,?,?,?,?,?,?,?);";
        try {
            if (file) {
                const imgPath = inFile(file,'img/User/');
                if (username) {
                    const result1 = await db.query(sql1, [username]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql2, [username,password,real_name,age,type,sex,phone,address,imgPath]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }else{
                if (username) {
                    const result1 = await db.query(sql1, [username]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql3, [username,password,real_name,age,type,sex,phone,address]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }
        } catch (error) {
            console.log(error, 'UserModel注册用户失败啦');
        }

    },
    GetUserList: async ({pageCurrent,pageSize,UserKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM user;';
        let sql2 = `SELECT COUNT(*) AS total FROM user WHERE CONCAT(username,real_name,phone,address) LIKE CONCAT('%', ?, '%')`;
        let sql3 = `SELECT id,username,password,real_name,age,type,sex,phone,address,avatar FROM user LIMIT ${current},${size}`;
        let sql4 = `SELECT id,username,password,real_name,age,type,sex,phone,address,avatar FROM user WHERE CONCAT(username,real_name,phone,address) LIKE CONCAT('%', ?, '%') LIMIT ${current},${size};`
        try {
            if(UserKeyword) {

                const res1 = await db.query(sql4, [UserKeyword]);
                const res2 = await db.query(sql2,[UserKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取用户列表总记录');
                console.log(res1[0], '获取用户列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取用户列表总记录');
                console.log(res2[0], '获取用户列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层用户列表数据出错啦');
        }
    },
    AddUser:async({username,password,real_name,age,type,sex,phone,address,avatar},file)=>{
        const sql1 = "SELECT * FROM car WHERE model = ? ";
        const sql2 = "INSERT INTO user (username,password,real_name,age,type,sex,phone,address,avatar) VALUES (?,?,?,?,?,?,?,?,?);";
        const sql3 = "INSERT INTO user (username,password,real_name,age,type,sex,phone,address) VALUES (?,?,?,?,?,?,?,?);";
        try {
            if (file) {
                const imgPath = inFile(file,'img/User/');
                if (username) {
                    const result1 = await db.query(sql1, [username]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql2, [username,password,real_name,age,type,sex,phone,address,imgPath]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }else{
                if (username) {
                    const result1 = await db.query(sql1, [username]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql3, [username,password,real_name,age,type,sex,phone,address]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }
        } catch (error) {
            console.log(error, 'UserModel添加用户失败啦');
        }
    },
    EditUser:async ({id,username,password,real_name,age,type,sex,phone,address,avatar},file) => {
        console.log(id,username,password,real_name,age,type,sex,phone,address,avatar,file,'修改接口访问了吗？')
        const sql1 = "UPDATE user SET username=?,password=?,real_name=?,age=?,type=?,sex=?,phone=?,address=?,avatar=? WHERE id=?;";
        const sql2 = "UPDATE user SET username=?,password=?,real_name=?,age=?,type=?,sex=?,phone=?,address=? WHERE id=?;";
        try {
            if (file) {
                const imgPath = inFile(file,'img/User/');
                if (id) {
                    const result2 = await db.query(sql1, [username,password,real_name,age,type,sex,phone,address,imgPath,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }else{
                if (id) {
                    const result2 = await db.query(sql2, [username,password,real_name,age,type,sex,phone,address,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }
        } catch (error) {
            console.log(error, 'userModel修改用户失败啦');
        }
    },
    DeleteUser: async ({id})=>{
        console.log(id,'删除用户id测试');
        const sql ='DELETE FROM user WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除用户调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    BatchDeleteUser: async  ({idList}) => {
        console.log(idList,'收集idList的数组');
        const sql = `DELETE FROM user WHERE id IN(${idList});`;
        try {
            if (idList.length>0){
                const res = await db.query(sql);
                console.log(res,'Model层批量删除用户调试');
                return res[0];
            }else{
                return ;
            }
        }catch (error){
            return{
                errorMsg:error
            }
        }
    },
    GetUserDetail:async ({id}) => {
        console.log(id,'用户id');
        const sql = 'SELECT id,username,password,real_name,age,type,sex,phone,address,avatar FROM user WHERE id = ? ;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'获取用户详情调试');
                return res[0];
            }else{
                return;
            }
        }catch (error) {
            console.log(error,'获取用户详情出错了——Model层');
        }
    }
};

module.exports = userModel;